//
//  YBCloudComputerBuyCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/30.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBCloudComputerBuyCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.addSubview(bgView)
        bgView.addSubviews([titleLabel, desLabel, priceLabel, unitPriceLabel, rightBgImgView])
        rightBgImgView.addSubview(rightLabel)
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(10)
            make.height.equalTo(25)
        }
        
        desLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.equalTo(20)
            make.height.equalTo(18)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(desLabel.snp.bottom).offset(4)
            make.height.equalTo(28)
            make.bottom.equalTo(-10)
        }
        
        unitPriceLabel.snp.makeConstraints { make in
            make.centerY.equalTo(priceLabel)
            make.right.equalTo(-19)
            make.height.equalTo(20)
        }
        
        rightBgImgView.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(48, 22))
            make.right.equalToSuperview()
            make.top.equalTo(15)
        }
        
        rightLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(1)
            make.centerY.equalToSuperview()
        }
        
        // 隐藏标签
        rightBgImgView.isHidden = true
        rightLabel.isHidden = true
    }
    
    /// 重写frame
    override var frame: CGRect {
        get { return super.frame }
        set {
            var frame = newValue
            frame.origin.x += 10
            frame.size.width -= 2*10
            super.frame = frame
        }
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            bgView.bk_addCornerBorder(radius: 4, borderWidth: 1.4, borderColor: kRGBColor(46, 143, 255))
        } else {
            bgView.bk_addCornerBorder(radius: 4, borderWidth: 0.1, borderColor: .clear)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var bgView: UIView = {
        let view = UIView(color: .lightWhiteDark27)
        view.bk_addCornerBorder(radius: 4, borderWidth: 0.1, borderColor: .clear)
        return view
    }()
    
    private lazy var titleLabel = self.bk_addLabel(font: .systemFont(ofSize: 18, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var desLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var priceLabel = self.bk_addLabel(bgColor: .clear, textColor: .red)
    
    private lazy var unitPriceLabel = self.bk_addLabel(bgColor: .clear, textColor: ColorDef.gray102, align: .right)
    
    private lazy var rightBgImgView: UIImageView = {
        var _image = UIImage.bk_gradient(size: CGSizeMake(48, 22), colors: [kRGBColor(88, 182, 255), kRGBColor(40, 128, 237)])
        _image = _image?.bk_freeRoundingCorners([.topLeft, .bottomLeft], radi: 11)
        let iv = UIImageView(image: _image)
        return iv
    }()
    
    private lazy var rightLabel = self.bk_addLabel(font: .systemFont(ofSize: 12), bgColor: .clear, textColor: .white, align: .right)
    
}

// MARK: - Public
extension YBCloudComputerBuyCell {
    
    func updateCell(by model: YBBuyModel) {
        titleLabel.text = model.buy + "卡"
        desLabel.text = "天总时长\(model.totalTime)小时/每天最多可用\(model.dailyTime)小时"
        priceLabel.attributedText = NSMutableAttributedString("¥") { $0
            .font(.systemFont(ofSize: 14))
        }.addText("\(model.priceTag)") { $0
            .font(.systemFont(ofSize: 24, weight: .bold))
        }
        
        unitPriceLabel.attributedText = NSMutableAttributedString("=\(model.price)") { $0
            .font(.systemFont(ofSize: 14))
            .foregroundColor(ColorDef.gray102)
        }.addText("元/天") { $0
            .font(.systemFont(ofSize: 10))
            .foregroundColor(ColorDef.gray102)
        }
        
        rightLabel.text = "安卓10"
    }
    
}
